package com.concurrency.example.deadLock.p2;

import java.awt.*;
import java.util.concurrent.TimeUnit;

/**
 * 出租车
 */
public class Taxi {
    /** 位置*/
    private Point location;
    /** 目的地*/
    private Point destination;

    private Dispatcher dispatcher;


    public Taxi(Point location, Point destination, Dispatcher dispatcher) {
        this.location = location;
        this.destination = destination;
        this.dispatcher = dispatcher;
    }

    public synchronized Point getLocation() {
        return location;
    }

    /**
     * 把GPS车辆位置写入系统
     * @param location
     */
    public synchronized void setLocation(Point location) throws InterruptedException{
        TimeUnit.SECONDS.sleep(1);
        this.location = location;
        if (location.equals(destination)) {
            dispatcher.notifyAvailable(this);
        }
    }
}
